package leetcode.year2021;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class _01CacheQuery {
  //  实现缓存功能，在N秒内如果使用相同的key查询，那么返回缓存值
   private static final Map<Integer,Integer> cache = new HashMap<>();
   private static final Map<Integer, LocalDateTime> timeMap = new HashMap<>();

    public int query(int key){
      int expiredSeconds = -10;
      if (!cache.containsKey(key)){
        int newAns = get_News(key);
        cache.put(key,newAns);
        timeMap.put(key,LocalDateTime.now());
      } else {
        LocalDateTime keyTime = timeMap.get(key);
        if (LocalDateTime.now().plusSeconds(expiredSeconds).isAfter(keyTime)) {
          int newAns = get_News(key);
          cache.put(key,newAns);
          timeMap.put(key,LocalDateTime.now());
        }
      }
      return cache.get(key);
    }

    public int get_News(int key){
      // 查询逻辑
      // cache.put(key,value)
      // timeMap.put(key,time);
      return 1;
    }
}
